package com.ita.osc.serviceImpl;

import java.util.Map;

import com.ita.osc.bean.Cart;
import com.ita.osc.bean.Dish;
import com.ita.osc.dao.CartDao;
import com.ita.osc.dao.DishDao;
import com.ita.osc.daoImpl.CartDaoImpl;
import com.ita.osc.daoImpl.DishDaoImpl;
import com.ita.osc.service.CartManager;

public class CartManagerImpl implements CartManager {
	
	CartDao cartDao = new CartDaoImpl();
	DishDao dishDao = new DishDaoImpl();
 
	@Override
	public Cart getCartByUserId(String uid) {
		// TODO Auto-generated method stub
		return cartDao.getCartByUserId(uid);
	}

	@Override
	public Cart removeDishFromCart(String did, Cart cart) {
		// TODO Auto-generated method stub
		Dish dish = dishDao.findDishById(did);
		Map<Dish, Integer> dishList = cart.getCartList();
		dishList.remove(dish);
		cart.setCartList(dishList);
		return cart;
	}

	@Override
	public Cart addDishIntoCart(String did, int quantity,Cart cart) {
		// TODO Auto-generated method stub
		Dish dish = dishDao.findDishById(did);
		Map<Dish, Integer> dishList = cart.getCartList();
		if(dishList.containsKey(dish)){
			dishList.put(dish, dishList.get(dish) + 1);
		}else {			
			dishList.put(dish, quantity);
		}
		cart.setCartList(dishList);
		return cart;
	}

	@Override
	public Cart updateQuantityOfDish(String did, int quantity, Cart cart) {
		// TODO Auto-generated method stub
		Dish dish = dishDao.findDishById(did);
		Map<Dish, Integer> dishList = cart.getCartList();
		dishList.remove(dish);
		if(quantity > 0){			
			dishList.put(dish, quantity);
		}
		cart.setCartList(dishList);
		return cart;
	}

	@Override
	public double getCartTotal(Cart cart) {
		// TODO Auto-generated method stub
		Map<Dish, Integer> dishList = cart.getCartList();
		double total = 0;
		for (Dish dish : dishList.keySet()) {
			total += dish.getPrice() * dishList.get(dish);
		}
		return total;
	}

	@Override
	public void clearCartByUserId(String uid) {
		// TODO Auto-generated method stub
		cartDao.removeCartListByUserId(uid);
	}

}
